layui.use(['form', 'table', 'eleTree', 'fsoft'], function () {
  var $ = layui.$
    , form = layui.form
    , table = layui.table
    , eleTree = layui.eleTree
    , fsoft = layui.fsoft;
  var tableId = "sysAreaTableBody";
  var searchFormId = "sysAreaSearchForm";
  //左侧树-20200217
  var areaTree = eleTree.render({
    elem: '#fsoftAreaTree'
    , method: 'post'
    , url: layui.cache['contentPath'] + '/sys-area/findTrees'
    , request: { name: "title", key: "id", children: "children" }
    , highlightCurrent: true
    // , defaultExpandAll: true
    , expandOnClickNode: false
    , showLine: true
    , done: function (res) {
      if (res.code == 0) {
        var data = res.data;
        if (data != undefined && data.length > 0) {
          areaTree.expandNode(data[0].id);
        }
      }
      active.doSearch();
    }
  });
  // 节点点击事件
  eleTree.on("nodeClick(fsoftAreaTree)", function (d) {
    var currentData = d.data.currentData;
    $("#parents").val(currentData.id);
    active.doSearch();
  });
  //搜索监听
  form.on('submit(sreach)', function (data) {
    active.doSearch(data.field);
    return false;
  });
  // 工具栏监听 （批量操作）
  var active = {
    doSearch: function (params) {
      //追加code、name查询条件
      if (params == undefined || params == null)
        params = $.extend({}, form.val(searchFormId));
      table.render({
        elem: '#' + tableId,
        title: '地区列表',
        where: params,
        toolbar: '#batchBtnToolbar',
        url: layui.cache['contentPath'] + '/sys-area/findList',
        cols: [[
          { type: 'checkbox', fixed: 'left', align: 'center', width: 60 }
          , { type: 'numbers', fixed: 'left', title: '序号', align: 'center', width: 60 }
          , { field: 'name', title: '名称', minWidth: 150, width: 180, sort: true }
          , { field: 'code', title: '编码', minWidth: 120, width: 150, sort: true }
          , { field: 'parentNames', title: '地区全称', minWidth: 150, width: 300 }
          , { field: 'postCode', title: '邮政编码', width: 95, align: 'center' }
          , {
            field: 'status', title: '状态', width: 70, align: 'center', templet: function (d) {
              if (d.status != undefined && d.status == 1) {
                return '<span class="layui-badge fsoft-bg-success">' + d.statusName + '</span>';
              }
              return '<span class="layui-badge fsoft-bg-info">' + d.statusName + '</span>';
            }
          }
          , { fixed: 'right', width: 120, align: 'center', title: '操作', toolbar: '#tbRowBar' }
        ]],
        page: { limit: 20, limits: [20, 30, 50, 80, 100], theme: '#1E9FFF' },
        height: 'full-103'
      });
    }
    , removeBatch: function (data) {
      if (data == undefined || data.length == 0) {
        layer.msg("请至少选择一条记录", { icon: 7, time: 3000 });
        return;
      }
      var ids = new Array();
      var havePrt = false;
      $.each(data, function (idx, item) {
        ids.push(item.id);
      });
      var msg = "确认要删除当前记录？";
      if (ids.length > 1) {
        msg = "确认要删除已选中的（" + ids.length + "）条记录？";
      }
      fsoft.confirm("询 问", msg, function (index) {
        fsoft.post(layui.cache['contentPath'] + '/sys-area/remove', ids, function (rs) {
          if (rs.code == 0) {
            fsoft.successMsg("删除成功！");
            areaTree.reload();
            active.doSearch();
          } else {
            fsoft.errorMsg("删除失败:" + rs.msg);
          }
        });
        layer.close(index);
      });
    }
    , showContentForm: function (data) {
      var path = layui.cache['contentPath'] + '/sys-area/add';
      if (data) {
        layui.cache['contentPath'] + '/sys-area/edit/' + data.id
      }
      layer.open({
        type: 2
        , area: ['500px', '400px']
        , content: path
        , end: function () {
          active.doSearch();
        }
      });
    }
  };
  table.on('toolbar(' + tableId + ')', function (obj) {
    var checkStatus = table.checkStatus(obj.config.id);
    switch (obj.event) {
      case 'add':
        active.showContentForm();
        break;
      case 'removeBatch':
        active.removeBatch(checkStatus.data);
        break;
    }
  });
  //行操作监听 
  table.on('tool(' + tableId + ')', function (obj) {
    var data = obj.data;
    if (obj.event === 'edit') {
      active.showContentForm(data);
    } else if (obj.event === 'del') {
      active.removeBatch([data]);
    }
  }); // End of 行监听 table.on
});